﻿/*********************************************
 创建者：  george
 创建时间：2017-10-14
 功能说明：选版人员评分权重设置
 ********************************************/
using com.nm;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using com.nm.library;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static OrderAPI.Model.UserWeightModel;

namespace OrderAPI
{
    [MustLogin]
    [Description("保存、获取选版人员评分权重数据")]
    public class UserWeightHandler : BaseHandler
    {
        [Description("保存选版人员评分权重")]
        [JsonType]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult Save([ParamDescription("参数列表")]SaveParam param)
        {
            if (param.list==null || param.list.Count == 0) return Error("参数行不能为空");
            var dc = new SysEntities();
            try
            {
                dc.t_order_userweight.Delete();
                foreach (var item in param.list)
                {
                    var data = new t_order_userweight
                    {
                        CompanyID = UserInfo.UserCompanyId,
                        ExtrSource = UserInfo.UserExtrSource,
                        CreateDate = DateTime.Now,
                        Creator = this.UserId,
                        ScoreRate = item.Weight,
                        UserID = item.UserID
                    };
                    dc.t_order_userweight.Add(data);
                }
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                Log.Error(ex);
                return Error(ex.Message);
            }
            return Success(0);
        }

        [Description("获取选版人员")]
        [ReturnType(typeof(IList<GetModel>))]
        public ApiResult GetUserWeightList()
        {
            var dc = new SysEntities();

            var query = from o in dc.t_order_userweight
                        join u in dc.sys_users on o.UserID equals u.UserID
                        where o.CompanyID == UserInfo.UserCompanyId && o.ExtrSource == UserInfo.UserExtrSource
                        select new { o.UserID, o.ScoreRate, u.UserName };
            var result = query.ToList().OrderBy(t => t.UserID).Select(t => new GetModel
            {
                UserID = t.UserID,
                Weight = t.ScoreRate,
                UserName = t.UserName
            }).ToList();
            return Success(result);
        }
    }
}
